package bjx.com.kehu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import bjx.com.kehu.R;
import bjx.com.kehu.contants.Constants;
import bjx.com.kehu.utils.SPUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jqc on 2017/10/25.
 */

public class SettingActivity extends BaseActivity {

    ImageView mIVBack;
    TextView mTVTitle;

    LinearLayout mLLPrice;
    LinearLayout mLLInsurance;
    LinearLayout mLLPartner;
    LinearLayout mLLLicence;
    LinearLayout mLLServiceTel;
    TextView mTVServiceTel;
    LinearLayout mLLPercent;
    LinearLayout mLLAbout;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        mIVBack = getViewById(R.id.back);
        mTVTitle = getViewById(R.id.title);
        mTVTitle.setText("设置");

        mLLPrice = getViewById(R.id.setting_ll_price);
        mLLInsurance = getViewById(R.id.setting_ll_insurance);
        mLLPartner = getViewById(R.id.setting_ll_partner);
        mLLLicence = getViewById(R.id.setting_ll_licence);
        mLLServiceTel = getViewById(R.id.setting_ll_serviceTel);
        mTVServiceTel = getViewById(R.id.setting_tv_tel);
        mLLPercent = getViewById(R.id.setting_ll_percent);
        mLLAbout = getViewById(R.id.setting_ll_about);
    }

    @Override
    protected void setListener() {
        mIVBack.setOnClickListener(this);
        mLLPrice.setOnClickListener(this);
        mLLInsurance.setOnClickListener(this);
        mLLPartner.setOnClickListener(this);
        mLLLicence.setOnClickListener(this);
        mLLServiceTel.setOnClickListener(this);
        mLLPercent.setOnClickListener(this);
        mLLAbout.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        mEngine.getCharge(userModel.getUser_id()).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String str = response.body().string();
                    JSONObject jo = new JSONObject(str);
                    int status = jo.getInt("status");
                    String msg = jo.getString("msg");
                    if (status == 200) {
                        mTVServiceTel.setText(jo.getJSONObject("result").getString("kf_tel"));
                    } else {
                        showToast("获取客服热线异常");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_ll_price: // 价格表
                String mParam = "";
                if (userModel != null) {
                    mParam = "fid=" + SPUtils.get(mApp, "fid", "-1");
                }
                startActivity(new Intent(mApp, WebViewActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        .putExtra("url", Constants.PRICE_URL)
                        .putExtra("param", mParam)
                        .putExtra("title", "价格表"));
                break;
            case R.id.setting_ll_insurance: // 保险
                startActivity(new Intent(mApp, WebViewActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).putExtra("url", Constants.INSURANCE_URL).putExtra("title", "保险"));
                break;
            case R.id.setting_ll_partner: // 合作伙伴说明
                startActivity(new Intent(mApp, WebViewActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).putExtra("url", Constants.PARTNEREXPLAN_URL).putExtra("title", "合作伙伴说明"));
                break;
            case R.id.setting_ll_licence: // 使用规则及代驾协议
                startActivity(new Intent(mApp, WebViewActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).putExtra("url", Constants.LIC_URL).putExtra("title", "协议规则"));
                break;
            case R.id.setting_ll_serviceTel: // 客服热线
                callTel(mTVServiceTel.getText().toString());
                break;
            case R.id.setting_ll_percent: // 个人资料设置
                startActivity(new Intent(mApp, PersonalActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
                break;
            case R.id.setting_ll_about: // 关于保驾行
                startActivity(new Intent(mApp, WebViewActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).putExtra("url", Constants.ABOUT_URL).putExtra("title", "关于我们"));
                break;
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }
}
